package com.hfi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfi.model.EmployeeMBG;
import com.hfi.model.EmployeeMBGExample;
import com.hfi.service.EmployeeBGMService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author ChangLiang
 * @date 2019/5/23
 */
@Controller
public class EmployeeBGMController {

    @Autowired
    private EmployeeBGMService employeeBGMService;

    /**
     * 可以进行任意的查询
     * @param employee
     * @return
     */
    @PostMapping("getEmployeeBGMByExample")
    @ResponseBody
    public PageInfo<EmployeeMBG> selectByExample(@RequestBody EmployeeMBG employee) {
        EmployeeMBGExample example = new EmployeeMBGExample();
        EmployeeMBGExample.Criteria criteria = example.createCriteria();
        if (employee.getId() != null) {
            criteria.andIdEqualTo(employee.getId());
        }
        if (StringUtils.isNotEmpty(employee.getLastName())) {
            criteria.andLastNameLike("%" + employee.getLastName() + "%");
        }
        if (StringUtils.isNotEmpty(employee.getEmail())) {
            criteria.andEmailLike("%" + employee.getEmail() + "%");
        }
        if (StringUtils.isNotEmpty(employee.getGender())) {
            criteria.andGenderEqualTo(employee.getGender());
        }
        if (employee.getDeptId()!=null) {
            criteria.andDeptIdEqualTo(employee.getDeptId());
        }
        PageHelper.startPage(employee.getPageNum(), employee.getPageSize());
        List<EmployeeMBG> employeeMBGS = employeeBGMService.selectByExample(example);
        PageInfo<EmployeeMBG> pageInfo = new PageInfo<>(employeeMBGS);
        return pageInfo;
    }
}
